import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/my/invoicing/fpHeader/viewModel/fpHeaderViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JFlatButton.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/contentView.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';

class FPHeaderView extends BaseStatefulWidget {
  final int id;

  ///编辑进来 传 id 其他 不传

  FPHeaderView({this.id});
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState

    return _FPHeaderViewSate();
  }
}

class _FPHeaderViewSate extends BaseState<FPHeaderViewModel, FPHeaderView> {
  ///抬头类型
  TextEditingController _ttlxCtr = TextEditingController();

  ///开票名称
  TextEditingController _kpmcCtr = TextEditingController();

  ///纳税人识别号
  TextEditingController _nsrsbhCtr = TextEditingController();

  ///开户行
  TextEditingController _khhCtr = TextEditingController();

  ///开户账号
  TextEditingController _khzhCtr = TextEditingController();

  ///客户地址
  TextEditingController _khdzCtr = TextEditingController();

  ///客户联系方式
  TextEditingController _khlxfsCtr = TextEditingController();

  ///设置默认抬头
  TextEditingController _setDefaultCtr = TextEditingController();

  ///是否默认 地址
  bool _isDefault;

  ///是否是编辑进来
  bool _isEdit;
  int _id;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _isDefault = false;
    _ttlxCtr.text = '企业单位';
    _isEdit = widget.id != null ? true : false;
    _id = widget.id;
    if (_isEdit) {
      // 编辑 才 执行
      Future.delayed(Duration.zero, () {
        _getData();
      });
    }
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return Scaffold(
      backgroundColor: SQColor.lightGray,
      appBar: MTopBar(
        title: '添加开票信息',
        rightButtonText: '保存',
        onRightButtonClick: () => _saveEvn(),
      ).create(),
      body: _bodyView(),
    );
  }

  _bodyView() {
    return SingleChildScrollView(
          child: Column(
        children: <Widget>[
          ContentView.textfield(
            controller: _ttlxCtr,
            mustType: "*",
            title: '抬头类型',
            readOnly: true,
          ),
          ContentView.textfield(
            controller: _kpmcCtr,
            mustType: "*",
            title: '开票名称',
            hintText: '请输入开票名称',
          ),
          ContentView.textfield(
            controller: _nsrsbhCtr,
            mustType: "*",
            title: '纳税人识别号',
            hintText: '请输入纳税人识别号',
            titleWidth: 152,
          ),
          ContentView.textfield(
            controller: _khhCtr,
            mustType: "*",
            title: '开户行',
            hintText: '请输入开户行',
          ),
          ContentView.textfield(
            controller: _khzhCtr,
            mustType: "*",
            title: '开户账号',
            hintText: '请输入开户账号',
          ),
          ContentView.textfield(
            controller: _khdzCtr,
            mustType: "*",
            title: '客户地址',
            hintText: '请输入客户地址',
          ),
          ContentView.textfield(
            controller: _khlxfsCtr,
            mustType: "*",
            title: '客户联系方式',
            hintText: '请输入客户联系方式',
            titleWidth: 152,
          ),
          ContentView.textfield(
            controller: _khlxfsCtr,
            mustType: " ",
            title: '设置为默认地址',
            hintText: '请输入客户联系方式',
            titleWidth: 168,
            readOnly: true,
            // suffixIcon: CheckBoxView(isDefault: _isDefault,),
            suffixIcon: _kRadioView(
                value: '是', title: '是', groupValueStr: _setDefaultCtr),
          ),
          SizedBox(
            height: 8,
          ),
          Offstage(
              offstage: !_isEdit,
              child: JFlatButton(
                  height: 44,
                  color: SQColor.white,
                  title: '删除开票信息',
                  textStyle: TextStyle(color: SQColor.red),
                  onPressed: () => _deleteEve()))
        ],
      ),
    );
  }

  ///复选框
  _kRadioView(
      {String value,
      String title,
      Color titleColor,
      TextEditingController groupValueStr}) {
    return Row(
      children: <Widget>[
        new Checkbox(
          value: _isDefault,
          activeColor: Colors.blue,
          onChanged: (bool val) {
            // val 是布尔值
            print(val);
            _setDefaultCtr.text = val.toString();
            setState(() {
              _isDefault = !_isDefault;
            });
          },
        ),
        JText(
          title,
          fontSize: 14,
          color: SQColor.textColorListSubTitle,
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
        )
      ],
    );
  }

  ///判断是否为空
  _judgeEmpty() {
    if (_ttlxCtr.text.isEmpty) {
      ToastUtils.showToast('抬头类型不能为空');
      return true;
    }
    if (_kpmcCtr.text.isEmpty) {
      ToastUtils.showToast('请填写开票名称');
      return true;
    }
    if (_nsrsbhCtr.text.isEmpty) {
      ToastUtils.showToast('请填纳税人识别号');
      return true;
    }
    if (_khhCtr.text.isEmpty) {
      ToastUtils.showToast('请填写开户行');
      return true;
    }

    if (_khzhCtr.text.isEmpty) {
      ToastUtils.showToast('请填写开户账号');
      return true;
    }
    if (_khdzCtr.text.isEmpty) {
      ToastUtils.showToast('请填写客户地址');
      return true;
    }
    if (_khlxfsCtr.text.isEmpty) {
      ToastUtils.showToast('请填写客户联系方式');
      return true;
    }

    return false;
  }

  _saveEvn() {
    if (_judgeEmpty()) {
      return;
    }
//id
int id = widget.id;
//抬头类型 抬头类型 0企业单位 string
    String headerType = '0'; //_ttlxCtr.text;
//开票名称 string
    String invoiceName = _kpmcCtr.text;
//纳税人识别号 string
    String taxplayerNumber = _nsrsbhCtr.text;
//开户行 string
    String accountBank = _khhCtr.text;
//开户行账号 string
    String account = _khzhCtr.text;
//客户地址 string
    String customerAddress = _khdzCtr.text;
//客户联系方式 string
    String customerTel = _khlxfsCtr.text;
//设置为默认地址 int  是否默认 0非默认 1默认
    int isDefault = _isDefault == false ? 0 : 1;

    var params = {
      'headerType': headerType,
      'invoiceName': invoiceName,
      'taxplayerNumber': taxplayerNumber,
      'accountBank': accountBank,
      'account': account,
      'customerAddress': customerAddress,
      'customerTel': customerTel,
      'isDefault': isDefault,
      'id': id,
    };

    print(params);

    viewModel.postSaveEvent(params: params);
    viewModel.saveStream().listen((event) {
      if (event['code'] == 0) {
        ToastUtils.showToast(event['msg']);

        Navigator.pop(context, '请求成功');
      }
    });
  }

  /// 编辑进来 获取开票信息
  _getData() async {
    viewModel.getDataEvent(_id);
    viewModel.getDataStream().listen((event) {
      if (event.code == 0 && event.data != null) {
        //赋值
        _ttlxCtr.text = event.data.headerType == null
            ? ''
            : (event.data.headerType == '0' ? '企业单位' : '个人');
        _kpmcCtr.text = event.data.invoiceName ?? '';
        _nsrsbhCtr.text = event.data.taxplayerNumber ?? '';
        _khhCtr.text = event.data.accountBank ?? '';
        _khzhCtr.text = event.data.account ?? '';
        _khdzCtr.text = event.data.customerAddress ?? '';
        _khlxfsCtr.text = event.data.customerTel ?? '';
        _isDefault = event.data.isDefault == null
            ? ''
            : (event.data.isDefault == 0 ? false : true);

        setState(() {});
      }
    });
  }

  ///删除
  _deleteEve() {
    viewModel.deleteEvent(widget.id);
    viewModel.deleteStream().listen((event) {
      if (event['code'] == 0) {
        ToastUtils.showToast(event['msg']);
        Navigator.pop(context, '请求成功');
      }else{
        ToastUtils.showToast(event['msg']);
      }
    });
  }
}

// class CheckBoxView extends StatefulWidget {
//   CheckBoxView({
//     Key key,
//     this.isDefault = false,
//   }) : super(key: key);

//   final bool isDefault;
//   //  bool get isDefault1 => isDefault;
//   // final String title;

//   @override
//   _CheckBoxViewState createState() => _CheckBoxViewState();
// }

// class _CheckBoxViewState extends State<CheckBoxView> {
//   bool _isDefault = false;

//   @override
//   void initState() {
//     // TODO: implement initState
//     super.initState();
//     _isDefault = widget.isDefault;
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Row(
//       children: <Widget>[
//         new Checkbox(
//           value: _isDefault,
//           activeColor: Colors.blue,
//           onChanged: (bool val) {
//             // val 是布尔值
//             print(val);
//             // _setDefaultCtr.text = val.toString();
//             setState(() {
//               _isDefault = !_isDefault;
//             });
//           },
//         ),
//         JText(
//           '是',
//           fontSize: 14,
//           color: SQColor.textColorListSubTitle,
//           overflow: TextOverflow.ellipsis,
//           maxLines: 1,
//         )
//       ],
//     );
//   }
// }
